/* indent size: 2 */

module.exports = (app) => {
  const DataTypes = app.Sequelize;

  const Model = app.model.define(
    "sys_user",
    {
      user_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        comment: "用户ID",
        autoIncrement: true,
      },
      dept_id: {
        type: DataTypes.BIGINT,
        allowNull: true,
        comment: "部门ID",
      },
      user_name: {
        type: DataTypes.STRING(30),
        allowNull: false,
        comment: "用户账号",
      },
      nick_name: {
        type: DataTypes.STRING(30),
        allowNull: false,
        comment: "用户昵称",
      },
      user_type: {
        type: DataTypes.STRING(2),
        allowNull: true,
        defaultValue: "00",
        comment: "用户类型（00系统用户）",
      },
      email: {
        type: DataTypes.STRING(50),
        allowNull: true,
        defaultValue: "",
        comment: "用户邮箱",
      },
      phonenumber: {
        type: DataTypes.STRING(11),
        allowNull: true,
        defaultValue: "",
        comment: "手机号码",
      },
      sex: {
        type: DataTypes.CHAR(1),
        allowNull: true,
        defaultValue: "0",
        comment: "用户性别（0男 1女 2未知）",
      },
      avatar: {
        type: DataTypes.STRING(100),
        allowNull: true,
        defaultValue: "",
        comment: "头像地址",
      },
      password: {
        type: DataTypes.STRING(100),
        allowNull: true,
        defaultValue: "",
        comment: "密码",
      },
      status: {
        type: DataTypes.CHAR(1),
        allowNull: true,
        defaultValue: "0",
        comment: "帐号状态（0正常 1停用）",
      },
      del_flag: {
        type: DataTypes.CHAR(1),
        allowNull: true,
        defaultValue: "0",
        comment: "删除标志（0代表存在 2代表删除）",
      },
      login_ip: {
        type: DataTypes.STRING(128),
        allowNull: true,
        defaultValue: "",
        comment: "最后登录IP",
      },
      login_date: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "最后登录时间",
      },
      create_by: {
        type: DataTypes.STRING(64),
        allowNull: true,
        defaultValue: "",
        comment: "创建者",
      },
      create_time: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "创建时间",
      },
      update_by: {
        type: DataTypes.STRING(64),
        allowNull: true,
        defaultValue: "",
        comment: "更新者",
      },
      update_time: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "更新时间",
      },
      remark: {
        type: DataTypes.STRING(500),
        allowNull: true,
        comment: "备注",
      },
    },
    {
      tableName: "sys_user",
      comment: "用户信息表",
    }
  );

  Model.associate = function () {
    app.model.SysUser.hasOne(app.model.SysDept, {
      foreignKey: "dept_id", //关联查询的字段
      sourceKey: "dept_id",
    });
    app.model.SysUser.hasMany(app.model.SysUserRole, {
      foreignKey: "user_id",
    });

    app.model.SysUser.belongsToMany(app.model.SysRole, {
      through: app.model.SysUserRole,
      foreignKey: "user_id",
    });
    app.model.SysUser.hasMany(app.model.SysUserPost, {
      foreignKey: "user_id",
    });
    app.model.SysUser.belongsToMany(app.model.SysPost, {
      through: app.model.SysUserPost,
      foreignKey: "user_id",
    });
  };

  return Model;
};
